﻿using UnityEngine;

namespace Battle
{
    public class ComboKit : MonoBehaviour
    {
        public UILabel comboNumber;
        private float timer;
        private const float SHOW_TIME = 1.8f;
        public UITweener tweener;

        float lastTime = -1f;
        public float durTime = 0.3f;

        public GameObject goodComboShow;
        public GameObject feverComboShow;
        public GameObject superComboShow;
        public GameObject hyperComboShow;

        void ShowComboDegree(ComboDegree d)
        {
            goodComboShow.SetActive(false);
            feverComboShow.SetActive(false);
            superComboShow.SetActive(false);
            hyperComboShow.SetActive(false);
            switch (d)
            {
                case ComboDegree.GOOD:
                    goodComboShow.SetActive(true);
                    break;
                case ComboDegree.FEVER:
                    feverComboShow.SetActive(true);
                    break;
                case ComboDegree.SUPER:
                    superComboShow.SetActive(true);
                    break;
                case ComboDegree.HYPER:
                    hyperComboShow.SetActive(true);
                    break;
            }
            
        }

        public enum ComboDegree
        {
            NONE,
            GOOD,
            FEVER,
            SUPER,
            HYPER
        }

        ComboDegree _curDegree = ComboDegree.NONE;
        public ComboDegree curDegree {
            get
            {
                return _curDegree;
            }
            set
            {
                if (_curDegree != value)
                {
                    ShowComboDegree(value);
                }
                _curDegree = value;
            }
        }

        public void ShowComboNumber(int number)
        {
            timer = 0;
            comboNumber.text = number.ToString();
            gameObject.SetActive(true);

            float scale = 1f;
            if (lastTime < 0f)
            {
            }
            else
            {
                float deltaTime = Time.time - lastTime;
                if (deltaTime >= durTime)
                {
                }
                else
                {
                    float scaleMax = 0.8f;
                    scale = 1 - (durTime - deltaTime) / durTime * scaleMax;
                }
            }

            tweener.GetComponent<TweenScale>().from = new Vector3(scale, scale, scale);
            tweener.GetComponent<TweenScale>().to = Vector3.one;
            tweener.ResetToBeginning();
            tweener.PlayForward();

            lastTime = Time.time;

            if (number < 10)
            {
                curDegree = ComboDegree.NONE;
            }
            else if (number >= 10 && number < 20)
            {
                curDegree = ComboDegree.GOOD;
            }
            else if (number >= 20 && number < 30)
            {
                curDegree = ComboDegree.FEVER;
            }
            else if (number >= 30 && number < 40)
            {
                curDegree = ComboDegree.SUPER;
            }
            else if (number >= 40)
            {
                curDegree = ComboDegree.HYPER;
            }
        }

        public void Hide()
        {
            gameObject.SetActive(false);
            lastTime = -1;
        }

        void Update()
        {
            timer += Time.deltaTime;
            if(timer >= SHOW_TIME)
            {
                Hide();
            }
        }
    }
}